# -*- coding: utf-8 -*-
"""
Created on Mon Mar 13 15:28:09 2017
@author: g
"""

import re
#Python中以r或R开头的的字符串表示（非转义的）原始字符串
print("\n1------#################\n")
string0 = 'This is a\nnormal string'
rawString0 = r'and this is a\nraw string'
print(string0)
print(rawString0)

print("\n2------#################\n")
#match()方法的工作方式是只有当被搜索字符串的开头匹配模式的时候它才能查找到匹配对象
string2 =re.match(r'dog', 'dog cat dog')
print(string2)
print(string2.group(0))
print(re.match(r'dog', 'dog cat dog'))
print(re.match(r'dog', 'dog cat dog').group(0))
print(string2.start())
print(string2.end())


print("\n3------#################\n")
#search()方法和match()类似，不过search()方法不会限制我们只从字符串的开头查找匹配
print(re.search(r'cat', 'dog cat dog'))
print(re.search(r'cat', 'dog cat dog').group(0))
print(re.search(r'cat', 'dog cat dog').start())
print(re.search(r'cat', 'dog cat dog').end())
string3 =re.search(r'cat', 'dog cat dog')
print(string3.start)
print(string3.end)

print("\n4------#################\n")
#findall()方法得到一个所有匹配模式的列表
print(re.findall(r'dog', 'dog cat dogdog cat dog'))

print("\n5------#################\n")
contactInfo = 'Doe, John: 555-1212'
match = re.search(r'(\w+), (\w+): (\S+)', contactInfo)
print(match)
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.group(3))

print("\n6------#################\n")

match = re.search(r'(?P<last>\w+), (?P<first>\w+): (?P<phone>\S+)', contactInfo)
print(match.group('last'))
print(match.group('first'))
print(match.group('phone'))

print("\n7------#################\n")
key = r"<html><body><h1>hello world<h1></body></html>"#这段是你要匹配的文本
p1 = r"(?<=<h1>).+?(?=<h1>)"
pattern1 = re.compile(p1)#我们在编译这段正则表达式
matcher1 = re.search(pattern1,key)#在源文本中搜索符合正则表达式的部分
print(matcher1.group(0)) #打印出来


print("\n8------#################\n")
key = r"NNN+20165133286875++_050601J02-001_A_A4_.zip"
p1 = r"(\d+).+?(\d+\S+-)"
pattern1 = re.compile(p1)#我们在编译这段正则表达式
matcher1 = re.search(pattern1,key)#在源文本中搜索符合正则表达式的部分
print("1:",matcher1.string) #打印出来
print("2:",matcher1.group(0)) #打印出来
print("3:",matcher1.group(1)) #打印出来
print("4:",matcher1.group(2)) #打印出来
#print("5:",matcher1.group(3)) #打印出来
print("\n9------#################\n")
p9 = r"(\d+).+?(\d+\w\d\d)"
pattern9 = re.compile(p9)#我们在编译这段正则表达式
matcher9 = re.search(pattern9,key)#在源文本中搜索符合正则表达式的部分
print("9-1:",matcher9.string) #打印出来
print("9-2:",matcher9.group(0)) #打印出来
print("9-3:",matcher9.group(1)) #打印出来
print("9-4:",matcher9.group(2)) #打印出来

print("\n10------#################\n")
p1 = r"(\_\d+\S+)"
pattern1 = re.compile(p1)#我们在编译这段正则表达式
matcher1 = re.search(pattern1,key)#在源文本中搜索符合正则表达式的部分
print("1:",matcher1.string) #打印出来
print("2:",matcher1.group(0)) #打印出来
print("3:",matcher1.group(1)) #打印出来
#print("4:",matcher1.group(2)) #打印出来

print("\n11------#################\n")
p11 = r"(\.\S+)"
pattern11 = re.compile(p11)#我们在编译这段正则表达式
matcher11 = re.search(pattern11,key)#在源文本中搜索符合正则表达式的部分
print("1:",matcher11.string) #打印出来
print("2:",matcher11.group(0)) #打印出来
print("3:",matcher11.group(1)) #打印出来
#print("4:",matcher1.group(2)) #打印出来
myName=matcher9.group(2)+matcher11.group(1)
print(myName)
'''
print("\n9------#################\n")
key = r"javapythonhtmlvhdl"#这是源文本
p1 = r"python"#这是我们写的正则表达式
pattern1 = re.compile(p1)#同样是编译
matcher1 = re.search(pattern1,key)#同样是查询
print(matcher1.group(0))
'''
print("\n12------#################\n")
p1 = r"(\d\d\d\d\d\d[A-Z]\d\d\S+\_)"
pattern12 = re.compile(p1)#我们在编译这段正则表达式
matcher12 = re.search(pattern12,key)#在源文本中搜索符合正则表达式的部分
print("12:",matcher12.string) #打印出来
print("12:",matcher12.group(0)) #打印出来

